package g_日期时间.LocalDateTime的使用.转成毫秒值;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 将LocalDateTime转成毫秒值
 *
 * @author xuyaxu
 * @date 2022/8/10 23:34
 */
public class Demo {
    public static void main(String[] args) throws ParseException {
        String dateTimeStr = "2022-08-10 23:38:25";

        // 获取 Date类型的毫秒值
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(dateTimeStr);
        // 1660145905000
        long dateMilliseconds = date.getTime();

        // 获取 LocalDateTime类型的毫秒值
        LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        // 1660145905000
        long toEpochMilli = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        System.out.println(toEpochMilli);
    }
}
